package org.example.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/files")
public class FilesController {
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        try {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            System.out.println("fileName: " + fileName);
            //上传文件到 /uploads/ 文件夹下
            //获取类的路径
            String path = this.getClass().getResource("/").getPath();
            file.transferTo(new java.io.File(path + fileName));
            return "upload success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "upload";
    }
}
